package dao;

import model.Doctor;
import model.FileModel;
import model.User;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: TM
 * Date: 11/18/12
 * Time: 11:12 AM
 * To change this template use FileModel | Settings | FileModel Templates.
 */
public class FileDaoImpl extends JdbcDaoSupport implements FileDao {
    public int uploadFile(final User user, final FileModel fileModel) {
        try {

            String seqQuery = "SELECT SEQ_FILE.nextval from dual";
            String sequence = getJdbcTemplate().queryForObject(seqQuery, String.class);

            String query = "INSERT INTO UPLOAD_INFOR (\n" +
                    "   UPLOAD_NO, NIC, PATH, \n" +
                    "   DESCRIPTION, UPLOAD_DATE) \n" +
                    "VALUES ( ?, ?, ?,\n" +
                    "    ?, sysdate )";
            System.out.println(query);
            return getJdbcTemplate().update(query, new Object[]{sequence, user.getNic(), fileModel.getPath(), fileModel.getDescription()});
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public List<FileModel> getFileList(final User user) {
        try {
            String query = "SELECT \n" +
                    "UPLOAD_NO, NIC, PATH, \n" +
                    "   DESCRIPTION, UPLOAD_DATE\n" +
                    "FROM UPLOAD_INFOR WHERE NIC = ? order by UPLOAD_DATE desc";
            System.out.println(query);
            return (List<FileModel>) getJdbcTemplate().query(query, new Object[]{user.getNic()},
                    new RowMapper() {
                        public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {

                            FileModel fileModel = new FileModel();
                            fileModel.setPath(resultSet.getString("PATH"));
                            fileModel.setDescription(resultSet.getString("DESCRIPTION"));
                            fileModel.setUploadNo(resultSet.getInt("UPLOAD_NO"));

                            return fileModel;
                        }

                    });
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }


    public FileModel getFilePathById(final String fileId) {
        try {
            String query = "SELECT \n" +
                    "UPLOAD_NO, NIC, PATH, \n" +
                    "   DESCRIPTION, UPLOAD_DATE\n" +
                    "FROM UPLOAD_INFOR WHERE UPLOAD_NO = ?";
            System.out.println(query);
            return (FileModel) getJdbcTemplate().queryForObject(query, new Object[]{fileId},
                    new RowMapper() {
                        public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {

                            FileModel fileModel = new FileModel();
                            fileModel.setPath(resultSet.getString("PATH"));
                            fileModel.setDescription(resultSet.getString("DESCRIPTION"));
                            fileModel.setUploadNo(resultSet.getInt("UPLOAD_NO"));
                            System.out.println("------"+resultSet.getString("PATH"));
                            return fileModel;
                        }

                    });
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

}
